﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using ThreeByte.ActiveDeck.Service;

//Based heavily on http://laharsub.codeplex.com

namespace ThreeByte.ActiveDeck.Publish.Model
{
    public class ActiveMessage
    {
        private static readonly XNamespace ns = ServiceConfiguration.NAMESPACE;
        
        public string Channel { get; set; }
        public int MessageId { get; set; }
        public XElement Body { get; set; }

        public XElement ToXml() {
            XElement node = new XElement(ns + "Message");
            node.Add(new XAttribute("Channel", Channel));
            if(MessageId != 0) {
                node.Add(new XAttribute("ID", MessageId));
            }
            node.Add(Body);
            return node;
        }

        public static ActiveMessage FromXml(XElement node) {
            ActiveMessage newMessage = new ActiveMessage();
            newMessage.Channel = node.Attribute("Channel").Value;
            if(node.Attribute("ID") != null) {
                newMessage.MessageId = int.Parse(node.Attribute("ID").Value);
            }
            newMessage.Body = node.Descendants().First();
            return newMessage;
        }
    }
}
